package com.kobeliu.controller;

import com.google.gson.Gson;
import com.kobeliu.api.interceptor.JwtCurrentUserInterceptor;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.exceptions.GraceException;
import com.kobeliu.pojo.Users;
import com.kobeliu.pojo.bo.ModifyUserBO;
import com.kobeliu.pojo.bo.SearchBO;
import com.kobeliu.pojo.vo.UsersVO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.UserService;
import com.kobeliu.utils.JwtUtils;
import com.kobeliu.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("userinfo")
@Slf4j
public class UserInfoController extends BaseInfoProperties {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @RequestMapping(method = RequestMethod.POST,path = "/modify")
    public GraceJSONResult modifyUserInfo(@RequestBody ModifyUserBO bo){

        if(StringUtils.isBlank(bo.getUserId())){
            GraceException.display(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        userService.modifyUserInfo(bo);

        //返回最新用户的信息
        UsersVO latest = getUserInfo(bo.getUserId());

        return GraceJSONResult.ok(latest);
    }

    private UsersVO getUserInfo(String userId){
        Users latest = userService.getUserById(userId);

        //其中有token一项 需要重新填入
        String jwt = jwtUtils.createJwtWithPrefix(new Gson().toJson(latest),TOKEN_USER_PREFIX);

        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(latest,usersVO);
        usersVO.setUserToken(jwt);

        return usersVO;

    }

    /**
     * 根据companyId查询与之关联的HR人数
     * @param companyId
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,path = "/getCountByCompanyId")
    public Long getCountByCompanyId(String companyId){
        return userService.getCountByCompanyId(companyId);

    }

    /**
     * 根据userID 和 companyId 绑定
     * @param userId
     * @param realName
     * @param companyId
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,path = "/bindingHRToCompany")
    public GraceJSONResult bindingHRToCompany(String userId,String realName,String companyId){

        //1.绑定信息
        userService.updateUserCompanyId(userId,realName,companyId);

        Users userById = userService.getUserById(userId);
        return GraceJSONResult.ok(userById.getMobile());
    }

    /**
     * 刷新用户信息 以及刷新token给前端
     * @param userId
     * @return
     */
    @PostMapping("refreshUserInfo")
    public GraceJSONResult freshUserInfo(@RequestParam("userId")String userId){

        UsersVO userInfo = getUserInfo(userId);

        return GraceJSONResult.ok(userInfo);
    }
    //changeUserToCand
    @PostMapping("changeUserToCand")
    public GraceJSONResult changeUserToCand(@RequestParam("hrUserId")String hrUserId){

        userService.changeUserToCand(hrUserId);

        return GraceJSONResult.ok();
    }

    @PostMapping("get")
    public GraceJSONResult get(@RequestParam("userId")String userId){

        UsersVO userInfo = getUserInfo(userId);
        return GraceJSONResult.ok(userInfo);
    }

    @PostMapping("getInfo")
    public GraceJSONResult getInfo(@RequestParam("userId")String userId){

        UsersVO userInfo = getUserInfo(userId);
        return GraceJSONResult.ok(new Gson().toJson(userInfo));
    }


    /**
     * 根据userId 将用户改成HR身份
     * @param hrUserId
     * @return
     */
    @PostMapping("changUser2Hr")
    public GraceJSONResult changUser2Hr(@RequestParam("hrUserId")String hrUserId){

        userService.updateUser2Hr(hrUserId);
        return GraceJSONResult.ok();
    }

    //======================= SAAS START=======================

    @PostMapping("saas/hrList")
    public GraceJSONResult hrList(Integer page,Integer limit){

        if(page == null) page=1;
        if(limit == null) limit=10;

        Users adminUsers = JwtCurrentUserInterceptor.currentUser.get();
        //根据userId去拿company
        if(adminUsers==null)
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);

        PagedGridResult pagedGridResult = userService.selectHrListByCompanyId(adminUsers.getHrInWhichCompanyId(), page, limit);

        return GraceJSONResult.ok(pagedGridResult);

    }
    //======================= SAAS END=========================


    //======================= Feign ==========================

    @PostMapping("getUserInfos")
    GraceJSONResult getUserInfos(@RequestBody SearchBO search){

        return GraceJSONResult.ok(new Gson().toJson(userService.getList(search.getUserIds())));
    }

    //======================= Feign ==========================

}
